package com.itboy.mall.pms.attr.controller;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itboy.mall.pms.attr.domain.MallAttr;
import com.itboy.mall.pms.attrattrgroup.domain.MallAttrAttrGroup;
import com.itboy.mall.pms.attrattrgroup.service.IMallAttrAttrGroupService;
import com.itboy.mall.pms.attrgroup.service.IMallAttrGroupService;
import com.itboy.mall.pms.attrgroup.vo.MallAttrGroupVo;
import com.itboy.mall.pms.category.service.IMallCategoryService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.itboy.common.annotation.RepeatSubmit;
import com.itboy.common.annotation.Log;
import com.itboy.common.core.controller.BaseController;
import com.itboy.common.core.domain.AjaxResult;
import com.itboy.common.enums.BusinessType;
import com.itboy.mall.pms.attr.vo.MallAttrVo;
import com.itboy.mall.pms.attr.bo.MallAttrQueryBo;
import com.itboy.mall.pms.attr.bo.MallAttrAddBo;
import com.itboy.mall.pms.attr.bo.MallAttrEditBo;
import com.itboy.mall.pms.attr.service.IMallAttrService;
import com.itboy.common.utils.poi.ExcelUtil;
import com.itboy.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 商品属性Controller
 *
 * @author itboy
 * @date 2021-07-24
 */
@Api(value = "商品属性控制器", tags = {"商品属性管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mall/attr")
public class MallAttrController extends BaseController {

    private final IMallAttrService iMallAttrService;

    private final IMallAttrGroupService iMallAttrGroupService;

    private final IMallAttrAttrGroupService iMallAttrAttrGroupService;

	private final IMallCategoryService iMallCategoryService;

    /**
     * 查询商品属性列表
     */
    @ApiOperation("查询商品属性列表")
    @PreAuthorize("@ss.hasPermi('mall:attr:list')")
    @GetMapping("/list")
    public TableDataInfo<MallAttrVo> list(@Validated MallAttrQueryBo bo) {
        return iMallAttrService.queryPageList(bo);
    }


	/**
	 * 查询未关联商品属性列表
	 */
	@ApiOperation("查询未关联商品属性列表")
	@GetMapping("/listByNoAttrId")
	public TableDataInfo<MallAttrVo> listByNoAttrId(@Validated MallAttrQueryBo bo){
		return iMallAttrService.listByNoAttrId(bo);
	}

    /**
     * 导出商品属性列表
     */
    @ApiOperation("导出商品属性列表")
    @PreAuthorize("@ss.hasPermi('mall:attr:export')")
    @Log(title = "商品属性", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<MallAttrVo> export(@Validated MallAttrQueryBo bo) {
        List<MallAttrVo> list = iMallAttrService.queryList(bo);
        ExcelUtil<MallAttrVo> util = new ExcelUtil<MallAttrVo>(MallAttrVo.class);
        return util.exportExcel(list, "商品属性");
    }

    /**
	 * 获取商品属性详细信息
	 */
	@ApiOperation("获取商品属性详细信息")
	@PreAuthorize("@ss.hasPermi('mall:attr:query')")
	@GetMapping("/{attrId}")
	public AjaxResult<MallAttrVo> getInfo(@NotNull(message = "主键不能为空")
										  @PathVariable("attrId") Long attrId) {
		MallAttrVo mallAttrVo = iMallAttrService.queryById(attrId);
		List<MallAttrGroupVo> attrGroupVoList = iMallAttrGroupService.getListByCatelogId(mallAttrVo.getCatalogId());
		String[] recursionById = iMallCategoryService.getRecursionById(mallAttrVo.getCatalogId());

		LambdaQueryWrapper<MallAttrAttrGroup> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(MallAttrAttrGroup::getAttrId,mallAttrVo.getAttrId());
		//关联表
		MallAttrAttrGroup attrAttrGroup = iMallAttrAttrGroupService.getOne(queryWrapper);
		if(ObjectUtil.isNotNull(attrAttrGroup)){
			mallAttrVo.setAttrGroupId(attrAttrGroup.getAttrGroupId());
		}
		mallAttrVo.setCatalogIds(recursionById);
		//分组集合
		mallAttrVo.setMallAttrGroupList(attrGroupVoList);
		return AjaxResult.success(mallAttrVo);
	}


	/**
	 * 按分类id查询销售属性
	 */
	@ApiOperation("按分类id查询销售属性")
	@GetMapping("getListByCatelogId/{catalogId}")
	public AjaxResult<List<MallAttr>> getListByCatelogId(@NotNull(message = "分类id不能为空")
										  @PathVariable("catalogId") Long catalogId) {
		return AjaxResult.success(iMallAttrService.getListByCatelogId(catalogId));
	}

    /**
     * 新增商品属性
     */
    @ApiOperation("新增商品属性")
    @PreAuthorize("@ss.hasPermi('mall:attr:add')")
    @Log(title = "商品属性", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody MallAttrAddBo bo) {
        return toAjax(iMallAttrService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改商品属性
     */
    @ApiOperation("修改商品属性")
    @PreAuthorize("@ss.hasPermi('mall:attr:edit')")
    @Log(title = "商品属性", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody MallAttrEditBo bo) {
        return toAjax(iMallAttrService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除商品属性
     */
    @ApiOperation("删除商品属性")
    @PreAuthorize("@ss.hasPermi('mall:attr:remove')")
    @Log(title = "商品属性" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{attrIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] attrIds) {
        return toAjax(iMallAttrService.deleteWithValidByIds(Arrays.asList(attrIds), true) ? 1 : 0);
    }
}
